package com.duobaoyu.dby.supply.admin.controller.config;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.duobaoyu.dby.channel.adapter.enums.OperationTypeEnum;
import com.duobaoyu.dby.common.core.util.R;
import com.duobaoyu.dby.common.core.validator.groups.Add;
import com.duobaoyu.dby.supply.admin.annotation.SupplySysLog;
import com.duobaoyu.dby.supply.biz.client.resource.ResourceStateMappingAdminClient;
import com.duobaoyu.dby.supply.biz.client.resource.bean.dto.StateMappingDTO;
import com.duobaoyu.dby.supply.biz.client.resource.bean.vo.StateMappingVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * 渠道状态映射管理
 * @author liyang
 * @version 1.0.0
 * @ClassName StateMappingController
 * @createTime 13:38:00 2023/04/21
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/state-mapping")
@Api(value = "state-mapping", tags = "状态映射")
public class StateMappingController {

	private final ResourceStateMappingAdminClient resourceStateMappingAdminClient;

	/**
	 * 添加状态映射信息
	 *
	 * @param stateMapping
	 * @return boolean
	 */
	@PostMapping("/create")
	@ApiOperation(value = "添加状态映射", notes = "添加状态映射信息")
	@SupplySysLog(value = "添加状态映射信息",operationType = OperationTypeEnum.INTERT)
	public R<Boolean> add(@Validated @RequestBody StateMappingDTO stateMapping) {

		return R.ok(resourceStateMappingAdminClient.addStateMapping(stateMapping));
	}

	/**
	 * 修改状态映射
	 *
	 * @param stateMapping
	 * @return boolean
	 */
	@PostMapping("/update")
	@ApiOperation(value = "修改状态映射", notes = "修改状态映射信息")
	@SupplySysLog(value = "修改状态映射信息",operationType = OperationTypeEnum.UPDATE)
	public R<Boolean> update(@Validated(Add.class) @RequestBody StateMappingDTO stateMapping) {

		return R.ok(resourceStateMappingAdminClient.updateStateMapping(stateMapping));
	}

	/**
	 * 删除状态映射
	 *
	 * @param id
	 * @return boolean
	 */
	@DeleteMapping("/delete/{id}")
	@ApiOperation(value = "删除状态映射", notes = "删除状态映射信息")
	@SupplySysLog(value = "删除状态映射信息",operationType = OperationTypeEnum.DELETE)
	public R<Boolean> delete(@NotNull(message = "状态映射ID不能为空") @PathVariable("id") Long id) {

		return R.ok(resourceStateMappingAdminClient.deleteStateMapping(id));
	}

	/**
	 * 查询状态映射信息详情
	 *
	 * @param id
	 * @return
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "查询状态映射信息详情", notes = "根据id查询状态映射信息详情")
	@SupplySysLog(value = "根据id查询状态映射信息详情",operationType = OperationTypeEnum.SELECT)
	public R<StateMappingVO> queryOne(@NotNull(message = "状态映射ID不能为空") @PathVariable("id") Long id) {

		return R.ok(resourceStateMappingAdminClient.queryStateMappingDetail(id));
	}

	/**
	 * 分页查询状态映射信息
	 *
	 * @param stateMapping
	 * @return
	 */
	@PostMapping("/page")
	@ApiOperation(value = "分页查询状态映射信息", notes = "分页查询状态映射信息")
	@SupplySysLog(value = "分页查询状态映射信息",operationType = OperationTypeEnum.SELECT)
	public R<Page<StateMappingVO>> page(@RequestBody StateMappingDTO stateMapping) {

		return R.ok(resourceStateMappingAdminClient.queryStateMappingPage(stateMapping));
	}


	/**
	 * 初始化本地缓存
	 *
	 * @return
	 */
	@GetMapping("/initCache")
	@ApiOperation(value = "状态映射初始化本地缓存", notes = "状态映射初始化本地缓存")
	@SupplySysLog(value = "状态映射初始化",operationType = OperationTypeEnum.UPDATE)
	public R<Boolean> initialization() {
		try {
			resourceStateMappingAdminClient.initializationRedisCache();
			return R.ok(true);
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

	/**
	 * 清除本地缓存
	 * @return
	 */
	@GetMapping("/clearCache")
	@ApiOperation(value = "清除本地缓存", notes = "清除本地缓存")
	@SupplySysLog(value = "清除本地缓存",operationType = OperationTypeEnum.DELETE)
	public R<Boolean> clearCache() {
		// 发送广播消息
		resourceStateMappingAdminClient.clearLocalCache();
		return R.ok(true);
	}
}

